class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right

def buildTree(preorder, inorder):
    def recur(root, left, right):
        if left > right:
            return None
        node = TreeNode(preorder[root])
        i = dic[preorder[root]]
        node.left = recur(root + 1, left, i - 1)
        node.right = recur(i - left + root + 1, i + 1, right)
        return node

    dic = {}
    for i in range(len(inorder)):
        dic[inorder[i]] = i
    return recur(0, 0, len(inorder) - 1)    